package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.product.service.SkuManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/admin/product")
public class SkuManageController {

    @Autowired
    private SkuManageService skuManageService;

    /**
     * 保存SkuInfo
     *
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }


    /**
     * sku分页列表
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuInfoList(@PathVariable Long page, @PathVariable Long limit, Long category3Id) {
        Page<SkuInfo> pageInfo = new Page<>(page, limit);
        IPage<SkuInfo> pageModel = skuManageService.getSkuInfoList(pageInfo, category3Id);
        return Result.ok(pageModel);
    }

    /**
     * 上架
     *
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId) {
        skuManageService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 上架
     *
     * @param skuId
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId) {
        skuManageService.cancelSale(skuId);
        return Result.ok();
    }

    /**
     * 根据skuId查询Sku
     *
     * @param skuId
     * @return
     */
    @GetMapping("/getSkuInfo/{skuId}")
    public Result getSkuInfo(@PathVariable Long skuId) {
        SkuInfo skuInfo = skuManageService.getSkuInfo(skuId);
        return Result.ok(skuInfo);
    }

    /**
     * 修改skuInfo信息
     *
     * @param skuInfo
     * @return
     */
    @PostMapping("/updateSkuInfo")
    public Result updateSkuInfo(@RequestBody SkuInfo skuInfo) {
        skuManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }


}
